/**
 * 
 */
package hichecker.university.jaxb;

import hichecker.university.Student;

import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * Class that transforms a HashMap into a more compact format, so the XML output is more readable
 */
public class HashMapAdapter extends XmlAdapter<CompactHash, HashMap<Student, Float>>{

	@Override
	public CompactHash marshal(HashMap<Student, Float> source) throws Exception {
		if (source == null) source = new HashMap<Student, Float>();
		
		CompactHash myHash = new CompactHash();
		List<StudentElement> list = myHash.getStudents();
		for (Entry<Student, Float> e : source.entrySet()) {
			list.add(new StudentElement(e.getKey().getID(), e.getValue()));
		}
		return myHash;
	}

	@Override
	public HashMap<Student, Float> unmarshal(CompactHash goal) throws Exception {
		HashMap<Student, Float> myHash = new HashMap<Student, Float>();
		for (StudentElement e : goal.getStudents()) {
			myHash.put(new Student(e.getKey()), e.getValue());
		}
		
		return myHash;
	}


}
